﻿using System;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.Client;
using System.IO;
using System.Windows.Forms;


namespace AutoMerge
{
    public class GetFileFromTFS
    {

        public static void get(string branchPath, RichTextBox tb)
        {
            try
            {
                tb.Clear();

                tb.AppendText("connecting to TFS..." + "\r\n");

                string tfsServer = "http://msnbc-tfs.interactive.msnbc.com:8080/";
                string machinePath = branchPath + "/Deployment/Deploy/Ijv.Platform.ManifestDriver.ConsoleApp/Manifests/dev/MachineManifest.config";
                string rolePath = branchPath + "/Deployment/Deploy/Ijv.Platform.ManifestDriver.ConsoleApp/Manifests/dev/RoleManifest.config";
                string actionPath = branchPath + "/Deployment/Deploy/Ijv.Platform.ManifestDriver.ConsoleApp/Manifests/dev/ActionManifest.config";
                Uri uri = new Uri(tfsServer);

                // use TfsTeamProjectCollection to talk to TFS 2008 and 2005
                TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(uri);
                tfs.EnsureAuthenticated();

                // Connect to TFS Source Control               
                VersionControlServer vcs = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));

                //get MachineManifest.config from TFS
                tb.AppendText("getting MachineManifest.config from " + branchPath + "..." + "\r\n");
                Item item = vcs.GetItem(machinePath);
                item.DownloadFile(@".\output\" + Path.GetFileName(item.ServerItem));

                //get RoleManifest.config from TFS
                tb.AppendText("getting RoleManifest.config from " + branchPath + "..." + "\r\n");
                item = vcs.GetItem(rolePath);
                item.DownloadFile(@".\output\" + Path.GetFileName(item.ServerItem));

                //get ActionManifest.config from TFS
                tb.AppendText("getting ActionManifest.config from " + branchPath + "..." + "\r\n");
                item = vcs.GetItem(actionPath);
                item.DownloadFile(@".\output\" + Path.GetFileName(item.ServerItem));
            }

            catch (Exception ex)
            {
                tb.AppendText("Encountering problem while getting file from TFS, make sure you run the application with Fareast account and the branch path is correct" + "\r\n");
                tb.AppendText(ex + "\r\n");
                throw new Exception("custom");
            }
        }
    }
}
